import clutter
import os
import ui
import gtk
import gobject
import threading
import zenlog
from gtk.gdk import threads_enter, threads_leave

SELECTION_NONE = -1
logger = zenlog.get_log('ui')

class VideoSelector(gobject.GObject):
    
    __gsignals__ = {
        'selection' : ( \
      gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT]) 
      )
    }
    
    def __init__(self):
        gobject.GObject.__init__(self)
        self._chosen_event = threading.Event()
        pass
    
    def choose_file(self):
        buttons = (
                   ("Send to Zen", 0)
                   )
        
        threads_enter()
        chooser = \
        self._chooser = \
            gtk.FileChooserDialog(
                                   title="Select a Video",
                                   action=gtk.FILE_CHOOSER_ACTION_OPEN,
                                   buttons=buttons
                                   )
        
        chooser.connect('response', self._file_chosen)
        chooser.set_current_folder('/home/nick/Examples')
        chooser.show()
        
        threads_leave()
        
        
        logger.info('waiting for selection')
        self._chosen_event.wait()
        logger.info('selection made')
        
        return self._selected
        
    def _file_chosen(self, target, response):
        
        logger.info('received event')
        
        if response == gtk.RESPONSE_NONE:
#            self.emit('selection', SELECTION_NONE)
            self._selected = None
        else:
            self._selected = target.get_filenames()
            
        self._chooser.destroy()
            
        logger.info('waking chosen_event waiting')
        self._chosen_event.set()
#            selected_filenames = target.get_filenames()
#            self.emit('selection', selected_filenames)

class FilePicker(clutter.Group):
    
    def __init__(self):
        clutter.Group.__init__(self)
        self._create_children()
    
    
    def _create_children(self):
        self._buttons = []
        
        # FIXME unix only
        icons = [
                 {'iconname' : 'folder_home', 'label' : 'Home', 'dir' : '~/.'}
                 ,
                 {'iconname' : 'desktop', 'label' : 'Desktop', 'dir' : '~/Desktop'}
                 ,
                 {'iconname' : 'folder', 'label' : 'Video', 'dir' : '~/Video'}
                 ]
        
        for icon in icons:
            button = ui.IconButton()
            button.set_icon_name(icon['iconname'])
            button.set_label(icon['label'])
            button.connect('clicked', self._on_icon_clicked, icon)
            self._buttons.append(button)
            self.add(button)
            button.show()
            
        self._file_list = FileList()
        self.add(self._file_list)
        self._file_list.show()
        self.layout()
    
    def _on_icon_clicked(self, event, icondata):
        dirlist = self._get_dir_contents(icondata)
        self._file_list.set_files(dirlist)
    
    def _get_dir_contents(self, icondata):
        # FIXME unix only
        userhome = os.environ.get('HOME')
        dir = icondata['dir']
        dir = dir.replace('~/', userhome + '/')
        
        dirlist = os.listdir(dir)
        files = []
        for fname in dirlist:
            print fname
            files.append(File(dir + "/" + fname))
            
        return files
        
        
    def _on_timeline_completed(timeline, frame_num, self):
        pass
    
    def layout(self):
        
        ctr = 0
        for icon in self._buttons:
            icon.set_position(0, ctr * 40)
            ctr = ctr + 1
            
        self._file_list.set_position(50, 0)

class File:
    
    def __init__(self, filename):
        self.filename = filename
        statresult = os.stat(filename)
        
    def get_filename(self):
        return self.filename
    
class FileList(clutter.Group):
    
    def __init__(self):
        clutter.Group.__init__(self)
        self._create_children()
    
    def _create_children(self):
        pass
    
    def set_files(self, file_array):
        
        self._file_labels = []
        for file in file_array:
            file_label = clutter.Label()
            self._file_labels.append(file_label)
            file_label.set_text(file.get_filename())
            self.add(file_label)
            file_label.show()
        
        self.layout()

    def layout(self):
        
        ypos = 0
        for label in self._file_labels:
            label.set_position(0, ypos)
            ypos += 30
        pass
    
    
if __name__ == '__main__':
    print 'yay'